<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author XPF
-->
<template>
  <div>
    <BasicModal v-bind="$attrs" width="1000px" @register="registerModal" @ok="handleSubmit">
      <template #title>
        <span> {{ getTitle.value }} </span>
      </template>
      <BasicTable @register="registerForm" @success="handleSuccess" />
    </BasicModal>
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewSportsSportsWeightAdditionalModal',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  //   import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { FormProps } from '/@/components/Form';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { SportsWeight, selectData } from '/@/api/sports/sportsWeight';
  import { SportsScoreGrade, saveSingleIndex } from '/@/api/sports/sportsScoreGrade';
  import { any } from 'vue-types';
  const emit = defineEmits(['success', 'register']);
  const { t } = useI18n('sports.sportsWeight');
  const record = ref<SportsWeight>({} as SportsWeight);
  const getTitle = computed(() => ({
    value: t('选择指标'),
  }));
  const tableColumns: BasicColumn[] = [
    {
      title: t('指标名称'),
      dataIndex: 'indexName',
      key: 'a_index_name',
      //sorter: true,
      align: 'left',
      dictType: 's_index_name',
      slots: { customRender: 'firstColumn' },
    },
    {
      title: t('适配年级'),
      dataIndex: 'adaptGrade',
      key: 'a_adapt_grade',
      //sorter: true,
      width: 430,
      align: 'left',
      dictType: 'vke_grade',
    },
    {
      title: t('权重'),
      dataIndex: 'weight',
      key: 'a.score_weight',
      width: 80,
      //sorter: true,
      align: 'right',
    },
  ];
  const [
    registerForm,
    { reload, getSelectRows, clearSelectedRowKeys, getSelectRowKeys, setSelectedRowKeys },
  ] = useTable({
    api: async (params) => {
      const res = await selectData(dataTable);
      return res.data;
    },
    beforeFetch: (params) => {
      params.pageSize = 10;
      params.adaptGrade = dataTable.adaptGrade;
      return params;
    },
    afterFetch: (res) => {
      // console.log(res);
      res.forEach((item) => {
        if (item.weight || item.weight == 0) {
          item.weight = item.weight + '%';
        } else {
          item.weight = '--';
        }
      });
    },
    columns: tableColumns,
    showIndexColumn: false,
    clickToRowSelect: true,
    rowSelection: { type: 'checkbox' },
    canResize: true,
  });
  let dataTable;
  const [registerModal, { closeModal, setModalProps }] = useModalInner(async (data) => {
    reload();
    setModalProps({ loading: true });
    setSelectedRowKeys(data.arr);
    dataTable = { adaptGrade: data.adaptGrade, adaptSex: data.adaptSex };
    setModalProps({ loading: false });
  });
  async function handleSubmit() {
    let data = getSelectRows();
    let arr = getSelectRowKeys();
    emit('save', data);
    clearSelectedRowKeys();
    closeModal();
  }
  function onSelectChange(selectedRowKeys) {
    selectedRowKeys = selectedRowKeys;
  }
  function handleSuccess() {}
</script>
